package com.gnerv.plug.commom;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

@Slf4j
public class FreeMarkerUtils {

    private static Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_23);

    static {
        CONFIGURATION.setDefaultEncoding("utf-8");
        CONFIGURATION.setClassForTemplateLoading(FreeMarkerUtils.class, "/templates");
    }

    public static String getContent(String templatePath, Map data) {
        Template template = null;
        try {
            template = CONFIGURATION.getTemplate(templatePath);
        } catch (IOException e) {
        }
        StringWriter result = new StringWriter();
        try {
            template.process(data, result);
        } catch (TemplateException e) {

        } catch (IOException e) {

        }
        return result.toString();
    }

}
